package com.wgj.android.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.tablayout.SlidingTabLayout;
import com.wgj.android.R;
import com.wgj.android.adapter.ShopStoreAdapter;
import com.wgj.android.adapter.ShopStoreGridAdapter;
import com.wgj.android.base.BannerHolderView;
import com.wgj.android.base.entity.Banner;
import com.wgj.android.entity.Kind;
import com.wgj.android.entity.ShopStore;
import com.wgj.android.fragment.ShopFragment;
import com.wgj.android.http.WGJAPI;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018\5\4 0004.
 */

public class ShopStoreActivity extends AppCompatActivity {
    @BindView(R.id.bannerView)
    ConvenientBanner bannerView;

    @BindView(R.id.tv_vip_tablayout)
    SlidingTabLayout tvVipTablayout;
    @BindView(R.id.tv_view_page)
    ViewPager tvViewPage;
    private List<ShopStore> shopStores = new ArrayList<>();
    private ShopStoreAdapter shopStoreAdapter;
    private ShopStoreGridAdapter shopStoreGridAdapter;
    private List<Kind> kinds = new ArrayList<>();
    private String title[];
    private MyPagerAdapter pageAdapter;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_shop_store1);
        ButterKnife.bind(this);
        //shopStoreGridAdapter = new ShopStoreGridAdapter(recyclerView, shopStores);

       /* recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setAdapter(shopStoreGridAdapter);*/
        loadBannerImgs();
        loadShopStores();
    }
    private void loadBannerImgs() {
        XHttp.obtain().get(WGJAPI.BASEURL + "slideUpImg/findBanner?group=5", null, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                List<Banner> banners = new ArrayList<>();
                JSONArray jsonArray = JSONArray.parseArray(s);
                for (int j = 0; j < jsonArray.size(); j++) {
                    JSONObject object = jsonArray.getJSONObject(j);
                    Banner banner = new Banner(object.getString("imgUrl"));
                    banners.add(banner);
                }
                bannerView.setPages(new CBViewHolderCreator<BannerHolderView>() {
                    @Override
                    public BannerHolderView createHolder() {
                        return new BannerHolderView();
                    }
                }, banners)
                        .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                        .startTurning(2000)
                        .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused});


            }

            @Override
            public void onFailed(int i, String s, boolean b) {

            }
        });
    }

    private void loadShopStores() {
        XHttp.obtain().get(WGJAPI.BASEURL + "goodsbrand/allByAll", null, new ListHttpCallBack<Kind>() {
            @Override
            public void onSuccess(List<Kind> list) {
                title=new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    title[i] = list.get(i).getBrandName();
                    ShopFragment shopFragment = new ShopFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("brandId", list.get(i).getBrandId());
                    shopFragment.setArguments(bundle);
                    //FragmentLowPrice shopFragment=new FragmentLowPrice();
                    fragments.add(shopFragment);
                }
                //getShopFragment();
                initview();
            }

            @Override
            public void onFailed(int i, String s, boolean b) {

            }
        });

    }

    private void initview() {
        pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
        tvViewPage.setAdapter(pageAdapter);
        //tvViewPage.setOffscreenPageLimit(8);
        tvVipTablayout.setViewPager(tvViewPage);
        //tvViewPage.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

        //tvViewPage.setCurrentItem()
    }


    public class MyPagerAdapter extends FragmentPagerAdapter {
        private View mCurrentView;
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

      /*  @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            if (object instanceof View) {
                mCurrentView = (View) object;
            } else if (object instanceof Fragment) {
                Fragment fragment = (Fragment) object;
                mCurrentView = fragment.getView();
            }
        }
        public View getPrimaryItem() {
            return mCurrentView;
        }*/
    }
}
